// pages/posts/posts_detail/post_detail.js

// const db = wx.cloud.database() //获取数据库的引用
// const abc = db.collection("post")
let res = null
// var postsDate = require('../../../postsdate/postsdate.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
    wx.showToast({

      title: '加载中',
      
      icon: 'loading'
      
      });
    // abc.get()
    // .then(res => {

    //    this.setData({
    //     postDate:res.data
    //    })
    // })
    // .catch(err => {
    //   console.error(err)
    // })
    res = await wx.cloud.callFunction({
      name: 'postsget'
    })

    this.setData({
      postDate: res.result.res.data
    })
    //查询首页的内容
    console.log(res.result.res)
    var postId = options.id;
    // console.log(postId);
    this.data.currentPostId = postId;
    //id获取在这里
    // var postDate = this.data.postsDate[postId];

    // console.log(postDate)
    // this.data.postDate = postDate;
    // this.setData({
    //  postDate : postDate
    // });
    this.setData({
      postId: postId
    });
    console.log(postId)
    var postsCollected = wx.getStorageSync('posts_collected')
    //判定是否被收藏或阅读过
    if (postsCollected) {
      //  var postCollected = postsCollected[postId]
      //  if(postCollected){
      //  this.setData({
      //    collected :postCollected
      //  })
      // }
      // }

      var postCollected = postsCollected[postId]
      if (postCollected) {
        this.setData({
          collected: postCollected
        })
      }
    } else {
      //新建一个缓存，为false
      //     var postsCollected = {};
      //     postsCollected[postId] = false;
      // wx.setStorageSync('posts_collected', postsCollected)
      var postsCollected = {};
      postsCollected[postId] = false;
      wx.setStorageSync('posts_collected', postsCollected);
    }
    
  },



//收藏按钮

  onColletionTap: function (event) {
    var postsCollected = wx.getStorageSync('posts_collected');
    var postCollected = postsCollected[this.data.currentPostId];
    //状态取反
    postCollected = !postCollected;
    postsCollected[this.data.currentPostId] = postCollected;
    //更新缓存
    wx.setStorageSync('posts_collected', postsCollected);
    this.setData({
      collected: postCollected
    })
    wx.showToast({
      title: postCollected ? "收藏成功" : "取消成功",
      duration: 1000
    })
  },
  //转发按钮
  onshareTap: function () {
    var that = this
    var itemList = ["微信", "朋友圈", "QQ"];
    wx.showActionSheet({
      itemList: itemList,
      itemColor: "#405f80",
      success: function (res) {
        //res.cancel,res.tapIndex
        console.log(res.tapIndex)
        if(res.tapIndex == 0){
          
          console.log("分享了微信")
          that.onShareAppMessage();
          
        
     
        }else{
        wx.showToast({
          title: "分享不到" + itemList[res.tapIndex],
          icon: "loading",
          duration: 1500
        })
      }
      }
    })
  },
  onShareAppMessage: function () {
 console.log("运行了分享")
    let url = encodeURIComponent('../posts/post_detail/post_detail?id=' + this.data.postId);
 
    return {
      title: "文章内容",
      path:`pages/posts/posts?url=${url}` 
    }
 
  }

,
  onMusicTop: function () {
    var currentPostId = this.data.currentPostId;
    //  var postDate = postsDate.postlist[currentPostId];
    var isPlayingMusic = this.data.isPlayingMusic;
    //  console.log(postDate.music.url)
    if (isPlayingMusic) {
      wx.pauseBackgroundAudio();
      this.setData({
        isPlayingMusic: false
      })
    } else {
      console.log(this.data.postDate)
      wx.playBackgroundAudio({

        dataUrl: this.data.postDate[this.data.postId].music.url,
        title: this.data.postDate[this.data.postId].music.title,
        coverImgUrl: this.data.postDate[this.data.postId].music.coverImgUrl,
      })
      this.setData({
        isPlayingMusic: true
      })
    }
  }



})